package com.example.keli.ui.userinfo;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;

import com.example.keli.R;
import com.example.keli.api.HttpUtils;
import com.example.keli.api.RetrofitManager;
import com.example.keli.base.BaseActivity;
import com.example.keli.base.utils.LogUtils;
import com.example.keli.base.widget.BladeView;
import com.example.keli.base.widget.PinnedHeaderListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;

/**
 * Description:
 * Data：2019/3/27-10:31
 * Author: Allen
 */
public class SelectAddressActivity extends BaseActivity {
    @BindView(R.id.listview)
    PinnedHeaderListView listview;
    @BindView(R.id.bladeview)
    BladeView bladeview;
    List<AddressData.ChildrenBean> childrens = new ArrayList<>();
    List<AddressData.ChildrenBean> sortChildrens = new ArrayList<>();

    // 首字母集
    private List<String> mSections;
    // 根据首字母存放数据
    private Map<String, List<String>> mMap = new HashMap<>();
    // 首字母位置集
    private List<Integer> mPositions = new ArrayList<>();
    // 首字母对应的位置
    private Map<String, Integer> mIndexer = new HashMap<>();

    SelectAddressAdapter addressAdapter;
    String[] sortList = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"};
    @Override
    protected int layoutId() {
        return R.layout.activity_select_address;
    }

    @Override
    protected void bindData() {
        abTitle.setText("地区选择");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getUserRegion();
    }

    private void getUserRegion() {
        HttpUtils.ApiFunc(RetrofitManager.mApiFunc.getUserRegion(), new SubscriberListener<BaseResponse<List<AddressData>>>() {
            @Override
            public void onSuccess(BaseResponse<List<AddressData>> listBaseResponse) {
                List<AddressData> addressDatas = listBaseResponse.getData();
                int size = addressDatas.size();
                for (int i = 0; i < size; i++) {
                    childrens.addAll(addressDatas.get(i).getChildren());
                }

                dealData();
            }

            @Override
            public void onFailer(String msg) {

            }

            @Override
            public void onTokenError() {

            }
        });
    }

    private static final String FORMAT = "^[A-Z].*$";
    private void dealData() {
        for (int i=0;i<sortList.length;i++){
            for (int j=0;j<childrens.size();j++){
                String firstName = childrens.get(j).getPinyin();
                if(sortList[i].contains(firstName)){
                    sortChildrens.add(childrens.get(j));
                }
            }
        }
        mSections = new ArrayList<>();
        mMap = new HashMap<>();
        mPositions = new ArrayList<>();
        mIndexer = new HashMap<>();

        if (sortChildrens != null && sortChildrens.size() > 0) {
            for (int i = 0; i < sortChildrens.size(); i++) {
                String firstName = sortChildrens.get(i).getPinyin();

                if (firstName.matches(FORMAT) || firstName.contains("#")) {
                    if (mSections.contains(firstName)) {
                        mMap.get(firstName).add(sortChildrens.get(i).getPinyin());
                    } else {
                        mSections.add(firstName);
                        List<String> list = new ArrayList<String>();
                        list.add(sortChildrens.get(i).toString());
                        mMap.put(firstName, list);
                    }
                }
            }
            int position = 0;
            for (int i = 0; i < mSections.size(); i++) {
                mIndexer.put(mSections.get(i), position);// 存入map中，key为首字母字符串，value为首字母在listview中位置
                mPositions.add(position);// 首字母在listview中位置，存入list中
                position += mMap.get(mSections.get(i)).size();// 计算下一个首字母在listview的位置
            }
        }


        bladeview.setOnItemClickListener(new BladeView.OnItemClickListener() {
            public void onItemClick(String s) {
                if (mIndexer.get(s) != null) {
                    listview.setSelection(mIndexer.get(s));
                }
            }
        });
        addressAdapter = new SelectAddressAdapter(SelectAddressActivity.this, sortChildrens, mSections, mPositions);
        listview.setAdapter(addressAdapter);
        listview.setOnScrollListener(addressAdapter);
        View view = LayoutInflater.from(SelectAddressActivity.this).inflate(R.layout.view_find_brand, listview, false);
        listview.setPinnedHeaderView(view);
        addressAdapter.setCallBack(new SelectCall() {
            @Override
            public void selectCall(String brand_id, String brand_name) {
                //拿到品牌id
                Intent intent = new Intent();
                intent.putExtra("pid", brand_id);
                intent.putExtra("label", brand_name);
                setResult(1001, intent);
                LogUtils.loge("品牌id是多少>>>>>>" + brand_id);
                finish();
            }
        });
    }

    public interface SelectCall {
        void selectCall(String brand_id,String brand_name);
    }

}
